class Notify < ActiveRecord::Base
  CHECK = {'未送审' => 0,'已送审' => 1,'已采用' => 2,'未采用' => 3}

  paginates_per 20

  validates_presence_of :title,:deleted,:content

  default_scope ->{ self.order("(notifies.deleted < now()) asc").order("pubtime desc") }
  scope :check,->(check){check.blank? ? nil : where(:check => check.to_i)}
  scope :submited_notifies,->{where(:check => [1,2,3])}
  scope :on_index,->(on_index){on_index.blank? ? nil : where(:on_index => on_index.to_i)}
  scope :notify_id,->(notify_id){notify_id.blank? ? nil : where(:id => notify_id.to_i)}
  scope :pubtime,->(begin_date,end_date){(begin_date.blank? && end_date.blank?) ? nil : where(:created_at => begin_date.to_date.beginning_of_day..end_date.to_date.at_end_of_day) }
  scope :contribution_units_id,->(contribution_units_id){contribution_units_id.blank? ? nil : where(:contribution_units_id => contribution_units_id)}
  
  def check_text
    #CHECK.map{|k,v| [v,k]}.to_h[check]
    case check
      when 0
        '未送审'
      when 1
        '已送审'
      when 2
        '已采用'
      when 3
        '未采用'
    end
  end

  #通知类型（我的通知，平台通知）
  def self.notify_type type,user
    case type
      when "1"
        self.where(:postby => user.name)
      when "2"
        if user.admin?
          self.submited_notifies
        else
          Notify.where("1=2")
        end
    end
  end















  def self.can_edit? notify,user
    if notify.check == 0 || user.admin?
      true
    else
      false
    end
  end

  def self.can_approve? notify,user
    if notify.check == 1 && user.admin?
      true
    else
      false
    end
  end
end
